<?php
/**
 * 好好读书
 * 遵循MT协议，开源并可商业使用，没有任何限制
 * @Author:
 * @Date: 2024/06/12 16:37
 * 官方惟一地址：
 */

namespace app\common\middleware;

use app\common\service\XcxJwtService;
use app\common\utils\ErrorCode;
use app\common\utils\Massage;
use app\common\utils\Res;

class XcxAuthCheckToken
{
    public function handle($request, \Closure $next)
    {
        if (empty($request->header('Authorization')) ) {
            return $next($request);
        }
        $token = trim(ltrim($request->header('Authorization'), 'Bearer'));
        if (empty($token)) {
            return Res::error(Massage::ROULE);
        }
        $result = XcxJwtService::checkToken($token);
        if ($result['code'] != ErrorCode::SUCCESS) {
            return Res::jsonResult($result);
        }
        $request->authInfo = $result;

        $request->id = $result['data']->id;
        $request->nickname = $result['data']->nickname;

        return $next($request);
    }
}
